/** 
 * \file gidamax.cu
 * \author Kyle E. Niemeyer
 * \date 10/04/2011
 *
 * Based on "idamax.f" from BLAS.
 *
 */

////////////////////////////////////////////////////////////////////////

/** gidamax finds index of element with maximum absolute value.
 *
 * \param[in]   n       number of elements in vector
 * \param[in]   dx      vector, dimension (n - 1) * |incx| + 1
 * \param[in]   incx    increment between elements of dx
 * \return      idamax  index of element with max. abs. value 
 */
__device__ __inline__ int gidamax ( int n, const double *dx, int incx )
{
  
  int idamax = 0;
  
  if ( ( n < 1 ) || ( incx <= 0 ) ) return idamax;
  
  idamax = 1;
  if ( n == 1 ) return idamax;
  
  if ( incx == 1 ) {
    
    // increment equal to 1
    
    dmax = fabs ( dx[0] );
    
    for ( uint i = 1; i < n; ++i ) {
      if ( fabs ( dx[i] ) > dmax ) {
        idamax = i;
        dmax = fabs ( dx[i] );
      }
    }
    
  } else {
    
    // increment not equal to 1
    
    uint ix = 1;
    dmax = fabs ( dx[0] );
    ix += incx;
    for ( uint i = 1; i < n; ++i ) {
      
      if ( fabs ( dx[ix] ) > dmax ) {
        idamax = i;
        dmax = fabs ( dx[ix] );
      }
      
      ix += incx;
    }
    
  } // end increment if
  
  return idamax;

} // end gdswap